﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonFramework.Utility;
using Xinkui.DataAccess.AutoGenerator;
using Xinkui.Common;
using Xinkui.WebModel.Helper;
using Xinkui.WebModel;

public partial class UserControl_UserCenter_UC_SendBoxDetail : Xinkui.WebModel.BasicControl.AccountControl {

    public int MessageId {
        get {
            return WebHelper.GetQueryStringValue<int>("Msg");
        }
    }

    public int Type {
        get {
            return WebHelper.GetQueryStringValue<int>("Mt", 0);
        }
    }

    public string AttachLink { get; set; }

    protected void Page_Load(object sender, EventArgs e) {
        AttachLink = string.Empty;
        if (Type == 0) {
            //SendBox
            var sendBoxInfo = Singleton<MessageSendBoxDataAccess>.Instance.MessageSendBox_View(MessageId);
            if (sendBoxInfo.UserID == this.CurrentLogin.UserID) {
                if (!string.IsNullOrEmpty(sendBoxInfo.Attach)) {
                    AttachLink = Xinkui.Common.FileUpload.GetDirectoryPath(sendBoxInfo.Attach, Xinkui.Common.Enum.ESourceType.FromMessage);
                }
                this.ltDate.Text = UIHelper.FormatDate(sendBoxInfo.SendDate);
                this.ltDetail.Text = sendBoxInfo.MessageContent.Replace("\\r", "<br/>"); 
                this.ltSubject.Text = sendBoxInfo.MessageTitle;
            }
            else {
                UIHelper.ShowErrorTips("The message can not view");
                return;
            }
        }
        else {
            //Inbox

            var inBoxInfo = Singleton<MessageInBoxDataAccess>.Instance.MessageInBox_View(MessageId);
            if (inBoxInfo.UserID == this.CurrentLogin.UserID) {
                var sendBoxInfo = Singleton<MessageSendBoxDataAccess>.Instance.MessageSendBox_View(inBoxInfo.MessageID);

                if (!string.IsNullOrEmpty(sendBoxInfo.Attach)) {
                    AttachLink = Xinkui.Common.FileUpload.GetDirectoryPath(sendBoxInfo.Attach, Xinkui.Common.Enum.ESourceType.FromMessage);
                }
                this.ltDate.Text = UIHelper.FormatDate(sendBoxInfo.SendDate);
                this.ltDetail.Text = sendBoxInfo.MessageContent.Replace("\\r","<br/>");
                this.ltSubject.Text = sendBoxInfo.MessageTitle;

            }
            else {
                UIHelper.ShowErrorTips("The message can not view");
                return;
            }
        }

    }

    public void btnDel_Click(object o, EventArgs e) {
        if (Type != 0) {
            Singleton<MessageInBoxDataAccess>.Instance.DeleteMessage(this.CurrentLogin.UserID, MessageId);
            UIHelper.ShowSuucessTips("The message has deleted", URLHelper.LinkToInBox());
        }
        else {
            Singleton<MessageSendBoxDataAccess>.Instance.DeleteMessage(this.CurrentLogin.UserID, MessageId);
            UIHelper.ShowSuucessTips("The message has deleted", URLHelper.LinkToSendBox());
        }

        
    }

    public void btnRepost_Click(object o, EventArgs e) {
        if (Type == 0) {
            Singleton<MessageSendBoxDataAccess>.Instance.RepostMessage(this.CurrentLogin.UserID, MessageId, WebHelper.GetFormValue<string>("txtmsg"));
            //UIHelper.ShowSuucessTips("The message has reponsted", URLHelper.LinkToSendBox());
        }
        else {
            UIHelper.ShowErrorTips("The message can not repost");
        }
    }
}